<?php

class Emms_Form_Loan_WriteOffPayment extends HTech_Form
{
    protected $_primary  = 'WriteOffPaymentId';
        
    private $_defaultLoanCode;
    private $_defaultClientFullName;
    private $_defaultClientCode;
    private $_defaultOfficerUsername;
    private $_defaultLocationName;
    private $_defaultWriteOffDate;
    private $_defaultWriteOffAmount;
    private $_defaultLegalCharges;
    private $_defaultLegalPayments;
    private $_defaultBalance;
    
    private function _init()
    {
        if(!$this->_request->isPost()) {
            if((int) $this->_request->getParam('id')) {
                try {
                    $payments = new LoanWriteOffPayments();
                    $payment = $payments->getRecord((int) $this->_request->getParam('id'))->d;
                } catch (Exception $e) {
                    echo $e->getMessage(); exit;
                }
                $loanId = $payment->LoanId;
            } else {
                $loanId = (int) $this->_request->getParam('loan');
            }
            try {
                $loan = new Emms_WrittenOffLoan($loanId);
            } catch (Exception $e) {
                echo $e->getMessage(); exit;
            }
            if($loan->get('Status') != 'Written-Off') {
                echo $this->translate("Invalid record request"); exit;
            }
            $this->_defaultLoanCode = $loan->get('LoanId');
            $this->_defaultClientFullName = $loan->get('ClientFullName');
            $this->_defaultClientCode = $loan->get('ClientCode');
            $this->_defaultOfficerUsername = $loan->get('OfficerUsername');
            $this->_defaultLocationName = $loan->get('LocationShortName');
            $this->_defaultWriteOffDate = $loan->get('WriteOffDate');
            $this->_defaultWriteOffAmount = $loan->get('Amount');
            $this->_defaultLegalCharges = $loan->get('LegalCharges');
            $this->_defaultLegalPayments = $loan->get('LegalPayments');
            $this->_defaultBalance = $loan->get('TotalBalance');
        } else {
            $this->_defaultBalance =  $this->_request->getParam('Balance');
        }
    }
                    
    public function getValues()
    {
        $data = parent::getValues();
        unset($data['LoanCode']);
        unset($data['ClientFullName']);
        unset($data['ClientCode']);
        unset($data['OfficerUsername']);
        unset($data['LocationName']);
        unset($data['WriteOffDate']);
        unset($data['WriteOffAmount']);
        unset($data['LegalCharges']);
        unset($data['LegalPayments']);
        unset($data['Balance']);
        return $data;
    }
    
    public function __construct() 
    {
        parent::__construct(); 
        $this->_init();    
        
        $this->setName('WriteOffPayment');
        
        $this->setAttrib('title', $this->translate("Write-off payment form"));
        $this->setDescription($this->translate("Add new payment"));
        
        $writeOffChargeId = new Zend_Form_Element_Hidden('WriteOffPaymentId'); 
        
        $loanId = new Zend_Form_Element_Hidden('LoanId');
        $loanId->setValue((int) $this->_request->getParam('loan'));
        
        $loanCode = new Zend_Form_Element_Text('LoanCode');
        $loanCode->setLabel("Loan code")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $clientCode = new Zend_Form_Element_Text('ClientCode');
        $clientCode->setLabel("Client code")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $clientFullName = new Zend_Form_Element_Text('ClientFullName');
        $clientFullName->setLabel("Client")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $officerUsername = new Zend_Form_Element_Text('OfficerUsername');
        $officerUsername->setLabel("Officer")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $locationName = new Zend_Form_Element_Text('LocationName');
        $locationName->setLabel("Branch")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $writeOffAmount = new Zend_Form_Element_Text('WriteOffAmount');
        $writeOffAmount->setLabel("Written-off amount")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $legalCharges = new Zend_Form_Element_Text('LegalCharges');
        $legalCharges->setLabel("Legal charges")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $legalPayments = new Zend_Form_Element_Text('LegalPayments');
        $legalPayments->setLabel("Legal payments")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $balance = new Zend_Form_Element_Text('Balance');
        $balance->setLabel("Current balance")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
        
        $writeOffDate = new Zend_Form_Element_Text('WriteOffDate');
        $writeOffDate->setLabel("Written-off date")
            ->setRequired(true)
            ->setAttrib('readonly', 'true');
                       
        $amount = new Zend_Form_Element_Text('Amount');
        $amount->setLabel("Payment amount")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Invalid amount")))
            ->addValidator('Between', true, array(
                'min' => 0,
                'max' => $this->_defaultBalance,
                'inclusive' => true,
                'messages' => sprintf($this->translate("Valid range from %s to %s"), 
                        0, $this->_defaultBalance))); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));        
        
        $this->addElements(array($loanCode, $clientFullName, $clientCode,  $officerUsername, 
            $locationName, $writeOffDate, $writeOffAmount, $legalCharges, $legalPayments, 
            $balance, $amount, $submit, $writeOffChargeId, $loanId));
        
        $this->setDefaults(array
            (
                'LoanCode'        => $this->_defaultLoanCode,
                'ClientFullName'  => $this->_defaultClientFullName,
                'ClientCode'      => $this->_defaultClientCode,
                'OfficerUsername' => $this->_defaultOfficerUsername,
                'LocationName'    => $this->_defaultLocationName,
                'WriteOffDate'    => $this->_defaultWriteOffDate,
                'WriteOffAmount'  => $this->_defaultWriteOffAmount,
                'LegalCharges'    => $this->_defaultLegalCharges,
                'LegalPayments'   => $this->_defaultLegalPayments,
                'Balance'         => $this->_defaultBalance
            ));
    }

}